package vikaa.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import vikaa.bean.Wowo;
import vikaa.timelinepackage.Timeline;
import vikaa.tools.DialogService;
import vikaa.tools.HttpUtil;
import vikaa.tools.ValueClass;
import vikaa.wowo.R;
import vikaa.wowo.cache.InvitorOpenHelper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WowoCreate1 extends Activity implements OnItemClickListener{
	private LinearLayout backBtn;
	private ListView wowolistview;
	private ArrayList<Wowo> mList = new ArrayList<Wowo>();
	public static boolean isfinish = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wowo_create1);
		initdata();
		initview();
		isfinish=false;
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(isfinish){
			finish();
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		WowoCreate1.this.deleteDatabase(InvitorOpenHelper.DB_NAME);
	}
	public void initview(){
		backBtn = (LinearLayout)findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		wowolistview = (ListView) findViewById(R.id.wowolist);
		wowolistview.setOnItemClickListener(this);
		WowoAdapter mAdapter = new WowoAdapter(WowoCreate1.this, mList, wowolistview);
        wowolistview.setAdapter(mAdapter);
	}
	public void initdata(){
		Wowo wowo1 = new Wowo(R.drawable.cover_memory_wowo,getString(R.string.create_memory_wowo),getString(R.string.create_memory_wowo_msg),true);
		Wowo wowo2 = new Wowo(R.drawable.cover_lover_wowo,getString(R.string.create_lover_wowo),getString(R.string.create_lover_wowo_msg),true);
		Wowo wowo3 = new Wowo(R.drawable.cover_baby_wowo,getString(R.string.create_baby_wowo),getString(R.string.create_baby_wowo_msg),true);
		Wowo wowo4 = new Wowo(R.drawable.cover_friend_wowo,getString(R.string.create_friend_wowo),getString(R.string.create_friend_wowo_msg),true);
		Wowo wowo5 = new Wowo(R.drawable.cover_other_wowo,getString(R.string.create_other_wowo),getString(R.string.create_other_wowo_msg),false);
		mList.add(wowo1);
		mList.add(wowo2);
		mList.add(wowo3);
		mList.add(wowo4);
		mList.add(wowo5);
	}
	public class WowoAdapter extends BaseAdapter{
	    private Context context;
		private LayoutInflater inflater;
		private final ArrayList<Wowo>  wbList; 
		private ListView listView;
	    
	    public WowoAdapter(Context context, ArrayList<Wowo> wbList,ListView listView) {
			super();
			this.context = context;
			this.inflater = LayoutInflater.from(context);
			this.wbList = wbList;
			this.listView = listView;
		}

		public int getCount() {
	        return wbList.size();
	    }

	    public Object getItem(int position) {
	        return wbList.get(position);
	    }

	    public long getItemId(int position) {
	        return position;
	    }
	    
	    public View getView(int position, View convertView, ViewGroup parent) {
	        convertView = inflater.inflate(R.layout.newwowo_item, null);
	        WeiBoHolder wh = new WeiBoHolder();
	        wh.wowo_cover = (ImageView) convertView.findViewById(R.id.wowo_cover);
	        wh.wowo_type = (TextView) convertView.findViewById(R.id.wowo_type);
	        wh.wowo_lock = (ImageView) convertView.findViewById(R.id.wowo_lock);
	        wh.wowo_introduction = (TextView) convertView.findViewById(R.id.wowo_introduction);
	        Wowo wb = wbList.get(position);
	        if(wb!=null){
	            wh.wowo_type.setText(wb.getType());
	            wh.wowo_cover.setImageResource(wb.getCover());
	            wh.wowo_introduction.setText(wb.getIntroduction());
	            if(!wb.isLock()){
	            	wh.wowo_lock.setVisibility(View.INVISIBLE);
	            }
	        }
	        return convertView;
	    }
	    class WeiBoHolder {
	    	public ImageView wowo_cover = null;
			public ImageView wowo_lock = null;
			public TextView wowo_type = null;
			public TextView wowo_introduction = null;
	    }
	}
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		if(position == 0){
			DialogService.showProgressDialog(WowoCreate1.this, getString(R.string.create_wowo));
			new WowoCreateThread().start();
		}
		else if(position<4 && position>0){
			Intent i = new Intent(WowoCreate1.this,WowoCreate3.class);
			i.putExtra("name", mList.get(position).getType().substring(2));
			i.putExtra("state", "1");
			startActivity(i);
		}
		else{
			startActivity(new Intent(WowoCreate1.this,WowoCreate2.class));
		}
		
	}
	Handler mhandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what){
			case 1:
				DialogService.closeProgressDialog();
				Mywowo.isrefresh = true;
				Intent intent = new  Intent(WowoCreate1.this,Timeline.class);
				intent.putExtra("sid", (String)msg.obj);
				intent.putExtra("role", String.valueOf(ValueClass.ROLE_CREATOR));
				intent.putExtra("count", "0");
				intent.putExtra("spacename", "回忆窝");
				intent.putExtra("cover", "");
				intent.putExtra("credits", "0");
				intent.putExtra("member", "0");
				intent.putExtra("host", "1");
				intent.putExtra("state", "1");
				startActivity(intent);
				Toast.makeText(getApplicationContext(), "创建窝窝成功", Toast.LENGTH_SHORT).show();
				finish();
				break;
			case 2:
				DialogService.closeProgressDialog();
				if(msg.obj != null){
					Toast.makeText(WowoCreate1.this, String.valueOf(msg.obj), 500).show();
				}
				break;
			case 3:
				DialogService.closeProgressDialog();
				Toast.makeText(WowoCreate1.this, getString(R.string.network_crash), 500).show();
				break;
			}
		}
	};
	class WowoCreateThread extends Thread{
    	boolean isrun = true;
    	@Override
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("spacename", "回忆窝");
    			map.put("state", "1");
    			String url = HttpUtil.BASE_URL + "space/create";
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(url, map));
					Log.i("adf",jsonObj.toString());
					String result = jsonObj.getString("result");
					if(result.equals("1")){
						String space_id = jsonObj.getJSONObject("info").getString("space_id");
						Message msg = mhandler.obtainMessage(1);
						msg.obj = space_id;
						mhandler.sendMessage(msg);
					}else if(result.equals("0")){
						Message msg = mhandler.obtainMessage(2);
						msg.obj = jsonObj.getJSONObject("info").getJSONArray("space").toString();
						mhandler.sendMessage(msg);
					}else{
						Message msg = mhandler.obtainMessage(3);
						mhandler.sendMessage(msg);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
    		}
    	}
    }
}
